package com.woniu.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.woniu.entity.Result;
import com.woniu.product.pojo.ChargePile;
import com.woniu.product.pojo.ChargeRecord;
import com.woniu.product.pojo.ChargeStation;
import com.woniu.product.mapper.ChargeMapper;
import com.woniu.product.entity.dto.FindChargeStationDTO;
import com.woniu.product.entity.dto.UserChargeDTO;
import com.woniu.product.service.IChargeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 充电站表 服务实现类
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-20
 */
@Service
public class ChargeServiceImpl extends ServiceImpl<ChargeMapper, ChargeStation> implements IChargeService {

    @Autowired
    private ChargeMapper chargeMapper;

    @Override
    public List<ChargeStation> searchStation(FindChargeStationDTO findChargeStationDTO) {
        List<ChargeStation> chargeStations = chargeMapper.searchStation(findChargeStationDTO);
        List<ChargeStation> afterHandle = new ArrayList<>();
        // 计算充电站与用户的距离
        for(ChargeStation chargeStation : chargeStations){
            // 计算X坐标差
            Double x = Math.pow(findChargeStationDTO.getPositionX() - chargeStation.getPositionX(), 2);
            // 计算Y坐标差
            Double y = Math.pow(findChargeStationDTO.getPositionY() - chargeStation.getPositionY(), 2);
            // 计算S距离差
            Double s = Math.sqrt(x + y);
            chargeStation.setDistance(s);
            if (chargeStation.getCount() != null) {
                afterHandle.add(chargeStation);
            }
        }
        return afterHandle.stream().sorted(Comparator.comparingDouble(ChargeStation::getDistance)).collect(Collectors.toList());
    }

    @Override
    public Result<Object> beginCharge(UserChargeDTO userChargeDTO) {
        if(userChargeDTO.getUserId() == null){
            return Result.error("获取绑定用户信息失败");
        }
        // 根据充电桩id查询充电桩状态
        ChargePile chargePile = chargeMapper.getPileStatus(userChargeDTO.getPileId());
        if(!"2".equals(chargePile.getStatus())){
            return Result.error("该充电桩不在可使用状态，请换一台充电桩试试");
        }
        // 保存充电记录生成订单
        Integer duration = userChargeDTO.getDuration();
        Date date = new Date();
        ChargeRecord chargeRecord = new ChargeRecord();
        chargeRecord.setId(IdWorker.getIdStr());
        chargeRecord.setPileId(userChargeDTO.getPileId());
        chargeRecord.setUserId(userChargeDTO.getUserId());
        chargeRecord.setOrderId(IdWorker.getIdStr());
        chargeRecord.setDuration(duration);
        chargeRecord.setAmount((new BigDecimal(duration)).multiply(chargePile.getChargePrice()));
        chargeRecord.setIsDeleted("0");
        chargeRecord.setCreatedTime(date);
        chargeRecord.setUpdatedTime(date);
        boolean status = chargeMapper.saveRecord(chargeRecord);
        if(!status){
            return Result.error("网络开小差了，请重试");
        }
        return Result.ok();
    }

}
